﻿Imports System.Reflection
Imports UCBase
Imports System.Collections.ObjectModel

Public Class Form1
    Private DeviceList As UCBase.DeviceList
    Private WithEvents Resources As New ResourceManager
    Private FileTools As New Files
    Private Sub LayoutTest_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim proc As Diagnostics.Process = Diagnostics.Process.GetCurrentProcess
        proc.PriorityClass = ProcessPriorityClass.AboveNormal
        AboveNormalToolStripMenuItem.Checked = True
        Resources.AppPath = Application.StartupPath
        Resources.MainTab = TabControl1
       

        If Resources.LoadSettings(Application.StartupPath) = False Then
            MsgBox("Please Set Paths")
            Dim X As New Paths
            X.PluginsPath = Application.StartupPath & "\Plugins"
            X.MacroPath = Application.StartupPath & "\Macro"
            X.ProjectsPath = Application.StartupPath & "\Projects"
            X.DataPath = Application.StartupPath & "\Data"
            X.SettingsPath = Application.StartupPath & "\Settings"
            If X.ShowDialog = Windows.Forms.DialogResult.OK Then
                Resources.PluginPath = X.PluginsPath
                Resources.MacroPath = X.MacroPath
                Resources.ProjectPath = X.ProjectsPath
                Resources.DataPath = X.DataPath
                Resources.SettingsPath = X.SettingsPath
                Resources.SaveSettings(Application.StartupPath)
            End If
        End If
        DeviceList = New UCBase.DeviceList(Resources.PluginSet, InsertToolStripMenuItem, AddressOf SourceItem_Click, Resources)
        If Resources.ScanPluginFolder Then
            DeviceList.ScanForPlugins()
        Else
            DeviceList.LoadPlugins()
        End If
        FileTools.SetFileAssociations()

        AddDiagramTab()
        TabControl1.SelectTab(0)
    End Sub

    Private Sub RealTimeToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RealTimeToolStripMenuItem.Click
        Dim proc As Diagnostics.Process = Diagnostics.Process.GetCurrentProcess
        proc.PriorityClass = ProcessPriorityClass.RealTime
        RealTimeToolStripMenuItem.Checked = True
        HighToolStripMenuItem.Checked = False
        AboveNormalToolStripMenuItem.Checked = False
        NormalToolStripMenuItem.Checked = False
        BelowNormalToolStripMenuItem.Checked = False
        IdleToolStripMenuItem.Checked = False

    End Sub

    Private Sub HighToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HighToolStripMenuItem.Click
        Dim proc As Diagnostics.Process = Diagnostics.Process.GetCurrentProcess
        proc.PriorityClass = ProcessPriorityClass.High
        RealTimeToolStripMenuItem.Checked = False
        HighToolStripMenuItem.Checked = True
        AboveNormalToolStripMenuItem.Checked = False
        NormalToolStripMenuItem.Checked = False
        BelowNormalToolStripMenuItem.Checked = False
        IdleToolStripMenuItem.Checked = False
    End Sub

    Private Sub AboveNormalToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboveNormalToolStripMenuItem.Click
        Dim proc As Diagnostics.Process = Diagnostics.Process.GetCurrentProcess
        proc.PriorityClass = ProcessPriorityClass.AboveNormal
        RealTimeToolStripMenuItem.Checked = False
        HighToolStripMenuItem.Checked = False
        AboveNormalToolStripMenuItem.Checked = True
        NormalToolStripMenuItem.Checked = False
        BelowNormalToolStripMenuItem.Checked = False
        IdleToolStripMenuItem.Checked = False

    End Sub

    Private Sub NormalToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NormalToolStripMenuItem.Click
        Dim proc As Diagnostics.Process = Diagnostics.Process.GetCurrentProcess
        proc.PriorityClass = ProcessPriorityClass.Normal
        RealTimeToolStripMenuItem.Checked = False
        HighToolStripMenuItem.Checked = False
        AboveNormalToolStripMenuItem.Checked = False
        NormalToolStripMenuItem.Checked = True
        BelowNormalToolStripMenuItem.Checked = False
        IdleToolStripMenuItem.Checked = False

    End Sub

    Private Sub BelowNormalToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BelowNormalToolStripMenuItem.Click
        Dim proc As Diagnostics.Process = Diagnostics.Process.GetCurrentProcess
        proc.PriorityClass = ProcessPriorityClass.BelowNormal
        RealTimeToolStripMenuItem.Checked = False
        HighToolStripMenuItem.Checked = False
        AboveNormalToolStripMenuItem.Checked = False
        NormalToolStripMenuItem.Checked = False
        BelowNormalToolStripMenuItem.Checked = True
        IdleToolStripMenuItem.Checked = False

    End Sub

    Private Sub IdleToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles IdleToolStripMenuItem.Click
        Dim proc As Diagnostics.Process = Diagnostics.Process.GetCurrentProcess
        proc.PriorityClass = ProcessPriorityClass.Idle
        RealTimeToolStripMenuItem.Checked = False
        HighToolStripMenuItem.Checked = False
        AboveNormalToolStripMenuItem.Checked = False
        NormalToolStripMenuItem.Checked = False
        BelowNormalToolStripMenuItem.Checked = False
        IdleToolStripMenuItem.Checked = True

    End Sub

    Private Sub SourceItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If sender.tag.ToString.ToLower.Contains(".mac") Then
            For Each C As Control In TabControl1.SelectedTab.Controls
                If TypeOf C Is Diagram Then
                    Dim D As Diagram = C
                    D.LoadUserDevice(sender.tag)
                End If
            Next
        Else
            Dim X As Control = DeviceList.GetDevice(sender.tag)
            If X IsNot Nothing Then
                For Each C As Control In TabControl1.SelectedTab.Controls
                    If TypeOf C Is Diagram Then
                        Dim D As Diagram = C
                        D.AddControl(X)
                        X.Left = 0
                        X.Top = 0
                        X.Visible = True
                    End If
                Next
            Else
                MsgBox("Empty")
            End If
        End If
    End Sub

    Private Sub PluginsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PluginsToolStripMenuItem.Click
    End Sub
    Private Sub ExitToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem1.Click
        Shutdown()
    End Sub
    Private Sub TabControl1_Selecting(ByVal sender As Object, ByVal e As System.Windows.Forms.TabControlCancelEventArgs) Handles TabControl1.Selecting
        If e.TabPageIndex = TabControl1.TabPages.Count - 1 Then
            AddDiagramTab()
        End If
    End Sub
    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
        AddDiagramTab()
    End Sub
    Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click
        TabControl1.TabPages.Remove(TabControl1.SelectedTab)
    End Sub
    Private Sub AddDiagramTab()
        Dim X As New TabPage("New Diagram")
        Dim Y As New Diagram
        Y.SetDeviceList(DeviceList)
        X.Controls.Add(Y)
        Y.Dock = DockStyle.Fill
        Y.GridColor = Color.White
        Y.ShowGrid = False
        X.BorderStyle = BorderStyle.Fixed3D
        X.ImageIndex = 0
        Y.DiagramName = "New Diagram"
        Y.Resources = Resources
        TabControl1.TabPages.Insert(TabControl1.TabPages.Count - 1, X)
    End Sub
    Private Sub AddUserDevice_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub ToolStripButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton3.Click
        Shutdown()
    End Sub
    Private Sub Shutdown()
        For Each T As TabPage In TabControl1.TabPages
            For Each C As Control In T.Controls
                If TypeOf C Is Diagram Then
                    Dim D As Diagram = C
                    D.ClearDiagram()
                End If
            Next
        Next
        Me.Close()
    End Sub
    Private Sub RefreshToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RefreshToolStripMenuItem.Click
        DeviceList.UnLoadPlugins()
        DeviceList.LoadPlugins(Resources.PluginSet)
        MsgBox("Plugins Refreshed", MsgBoxStyle.Information, "Refresh Plugins")

    End Sub

    Private Sub UnloadToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UnloadToolStripMenuItem.Click
    End Sub

    Private Sub ReloadToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ReloadToolStripMenuItem.Click
    End Sub

    Private Sub FilterTestToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub AddControlToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddControlToolStripMenuItem.Click
        For Each C As Control In TabControl1.SelectedTab.Controls
            If TypeOf C Is Diagram Then
                Dim D As Diagram = C
                'Dim Cn As New DeveloperFrame.BaseCamera
                'D.AddControl(Cn)
            End If
        Next

    End Sub

    Private Sub PathsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim X As New Paths
        X.PluginsPath = Resources.PluginPath
        X.MacroPath = Resources.MacroPath
        X.ProjectsPath = Resources.ProjectPath
        X.DataPath = Resources.DataPath
        X.SettingsPath = Resources.SettingsPath
        If X.ShowDialog = Windows.Forms.DialogResult.OK Then
            Resources.PluginPath = X.PluginsPath
            Resources.MacroPath = X.MacroPath
            Resources.ProjectPath = X.ProjectsPath
            Resources.DataPath = X.DataPath
            Resources.SettingsPath = X.SettingsPath
            Resources.SaveSettings(Application.StartupPath)
        End If

    End Sub

    Private Sub SaveAsDeviceToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveAsDeviceToolStripMenuItem.Click
        Dim X As New SaveDevice
        For Each C As Control In TabControl1.SelectedTab.Controls
            If TypeOf C Is Diagram Then
                Dim D As Diagram = C
                For Each CD As Control In D.Devices
                    If CD.GetType().GetInterface("I_LoadSave", True) IsNot Nothing Then
                        X.AddDevice(CD.Name)
                    End If
                Next
            End If
        Next

        For Each I As ToolStripMenuItem In InsertToolStripMenuItem.DropDownItems
            X.AddMenu(I.Text)
        Next

        If X.ShowDialog = Windows.Forms.DialogResult.OK Then
            For Each C As Control In TabControl1.SelectedTab.Controls
                If TypeOf C Is Diagram Then
                    Dim D As Diagram = C
                    D.SaveAsDevice(X.DeviceName, X.SelectedDevice, X.SelectedMenu)
                End If
            Next
        End If

    End Sub

    Private Sub CreateDirectoryStructuresToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub OptionsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OptionsToolStripMenuItem.Click
        Dim X As New OptionsDialog(Resources)
        If X.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Exit Sub
        End If
    End Sub

    Private Sub Resources_DevelopmentModeChanged(ByVal NewState As Boolean) Handles Resources.DevelopmentModeChanged
        DeveloperToolStripMenuItem.Visible = NewState

    End Sub

    Private Sub CreateDirectoryStructuresToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CreateDirectoryStructuresToolStripMenuItem1.Click
        If FB1.ShowDialog = Windows.Forms.DialogResult.OK Then
            Dim P As String = FB1.SelectedPath
            If P.EndsWith("\") Then
                P = FB1.SelectedPath
            Else
                P = FB1.SelectedPath & "\"
            End If
            IO.Directory.CreateDirectory(P & "Plugins")
            IO.Directory.CreateDirectory(P & "Macros")
            IO.Directory.CreateDirectory(P & "Projects")
            IO.Directory.CreateDirectory(P & "Data")
            IO.Directory.CreateDirectory(P & "Settings")
            If MsgBox("Update settings to new structure paths?", MsgBoxStyle.YesNo, "Update Paths") = MsgBoxResult.Yes Then
                Resources.PluginPath = P & "Plugins"
                Resources.MacroPath = P & "Macros"
                Resources.ProjectPath = P & "Projects"
                Resources.DataPath = P & "Data"
                Resources.SettingsPath = P & "Settings"
                Resources.SaveSettings(Application.StartupPath)
            End If

        End If

    End Sub

    Private Sub DeleteSettingsFileToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteSettingsFileToolStripMenuItem.Click
        IO.File.Delete(IO.Path.Combine(Application.StartupPath, "UniSet.ini"))
        MsgBox("Settings File Deleted", MsgBoxStyle.OkOnly, "Delete Settings File")
    End Sub

    Private Sub DeleteDirectoryStructuresToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteDirectoryStructuresToolStripMenuItem.Click
        If FB1.ShowDialog = Windows.Forms.DialogResult.OK Then
            Dim P As String = FB1.SelectedPath
            If P.EndsWith("\") Then
                P = FB1.SelectedPath
            Else
                P = FB1.SelectedPath & "\"
            End If
            IO.Directory.Delete(P & "Plugins")
            IO.Directory.Delete(P & "Macros")
            IO.Directory.Delete(P & "Projects")
            IO.Directory.Delete(P & "Data")
            MsgBox("Folder Structures Removed", MsgBoxStyle.OkOnly, "Remove Folder Structures")
        End If
    End Sub

    Private Sub UnLoadPluginsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UnLoadPluginsToolStripMenuItem.Click
        DeviceList.UnLoadPlugins()
    End Sub

    Private Sub LoadPluginsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LoadPluginsToolStripMenuItem.Click
        DeviceList.LoadPlugins(Resources.PluginSet)
    End Sub

    Private Sub ReloadPluginsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ReloadPluginsToolStripMenuItem.Click
        DeviceList.UnLoadPlugins()
        DeviceList.LoadPlugins(Resources.PluginSet)
    End Sub

    Private Sub TabPage2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabPage2.Click

    End Sub

    Private Sub PluginsToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PluginsToolStripMenuItem1.Click

    End Sub
End Class
